package com.huahua.api.controller.developUtil;

import com.huahua.api.entity.base.R;
import com.huahua.api.entity.developTool.github.vo.GithubGetBranchListVO;
import com.huahua.api.entity.developTool.github.vo.GithubObtainInfoVo;
import com.huahua.api.entity.param.GithubBaseParam;
import com.huahua.api.service.GithubService;
import com.huahua.api.util.OwnGithubUtil;
import lombok.RequiredArgsConstructor;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2025年02月27日 13:13
 * @description believe in yourself
 */
@RestController
@RequestMapping("/develop/github")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class GithubController {

    private final GithubService githubService;
    /**
     * 获取该地址的所有分支
     *
     * @param param param
     * @return {@link Object }
     * @throws GitAPIException git apiexception
     * @throws IOException     IOException
     */
    @PostMapping("/getBranch")
    public Object getBranch(@RequestBody GithubBaseParam param) throws GitAPIException, IOException {
        String removeSuffix = OwnGithubUtil.removeSuffix(param.getGithubAddress());
        param.setGithubAddress(removeSuffix);
        List<GithubGetBranchListVO> branches = githubService.getBranches(param);
        return R.success(branches);
    }


    /**
     * 根据地址 判断出具体类型（如不带分支的仓库地址|带分支的仓库地址|已发行文件地址）
     *
     * @param param param
     * @return {@link Object }
     */
    @PostMapping("/info")
    public Object getInfo(@RequestBody GithubBaseParam param) throws GitAPIException, IOException {
        String removeSuffix = OwnGithubUtil.removeSuffix(param.getGithubAddress());
        param.setGithubAddress(removeSuffix);
        GithubObtainInfoVo info = githubService.getInfo(param);
        return R.success(info);
    }


    /**
     * 下载
     *
     * @param param param
     * @return {@link Object }
     * @throws GitAPIException git apiexception
     * @throws IOException     IOException
     */
    @PostMapping("/download")
    public Object download(@RequestBody GithubBaseParam param) throws GitAPIException, IOException {
        String removeSuffix = OwnGithubUtil.removeSuffix(param.getGithubAddress());
        param.setGithubAddress(removeSuffix);
        String download = githubService.download(param);
        return R.success(download);
    }



    /**
     * 下载
     *
     * @param param param
     * @return {@link Object }
     * @throws GitAPIException git apiexception
     * @throws IOException     IOException
     */
    @PostMapping("/downloadZip")
    public Object downloadZip(@RequestBody GithubBaseParam param) throws GitAPIException, IOException {
        String removeSuffix = OwnGithubUtil.removeSuffix(param.getGithubAddress());
        param.setGithubAddress(removeSuffix);
        String download = githubService.downloadZip(param);
        return R.success(download);
    }
}
